﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESF.SSO.Core.Entities;
using ESF.SSO.Presenter.Admin.Enterprise;
using ESF.Web.Pages;
using ESF.General;
using ESF.Web;
using ESF.Web.ServerControls;
using ESF.SSO.Core.Entities.BasicReferences;

#endregion


public partial class Enterprise_BrowseEnterprises : BaseCustomBrowsePagedPage<Enterprise, BrowseEnterprisesPresenter, IBrowseEnterprisesViewer>, IBrowseEnterprisesViewer
{
	#region Carga Inicial
	
	protected void Page_Load(object sender, EventArgs e)
	{
		_Presenter = new BrowseEnterprisesPresenter (this);
	
		if (!IsPostBack)
		{
			lblTitle.Text = Title;
			
			_Presenter.Load ();

			ListLoader.LoadList (ddlIdentificationType, WebGeneral.ListBasicReference (BasicReferenceTypes.IdentificationType));
			
			btnNew.Attributes ["onclick"] = JS.GenerateGoToFinal ("EditEnterprise.aspx");
		}
	}
	
	#endregion


	#region IBrowseEnterprisesViewer Members

	public int? IdentificationTypeID
	{
		get
		{
			return TextHelper.GetNullableInt (ddlIdentificationType.SelectedValue);
		}
		set
		{
			ListLoader.Select (ddlIdentificationType, value);
		}
	}

	public string IdentificationNumber
	{
		get
		{
			return txtIdentificationNumber.Text;
		}
		set
		{
			txtIdentificationNumber.Text = value;
		}
	}

	public string Name
	{
		get
		{
			return txtName.Text;
		}
		set
		{
			txtName.Text = value;
		}
	}

	#endregion
	
	
	#region Funciones Sobreescritas

	protected override Button GetDeleteButton()
	{
		return btnDelete;
	}


	public override IEnumerable<Enterprise> GetSelectedItems(IEnumerable<string> pIDs)
	{
		foreach (string ID in base.SelectedIDs)
		{
			yield return new Enterprise () { ID = TextHelper.GetInt (ID) };
		}
	}

	protected override DataPagging GetDataPagging()
	{
		return dpggrvResults;
	}


	protected override GridView GetGridView()
	{
		return grvResults;
	}
	
	#endregion
	
	
	#region Eventos de la Grilla
	
	protected void grvResults_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		Enterprise item;
		
	
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			item = e.Row.DataItem as Enterprise;
		
			SetHiddenFieldForID (e.Row, item.ID.Value);
			
			(e.Row.FindControl ("lblIdentificationNumber") as Label).Text = string.Format ("{0} - {1}", item.IdentificationNumber, item.IdentificationNumberDigit);
			
			(e.Row.FindControl ("lblIdentificationTypeCode") as Label).Text = item.IdentificationType.Code;
		}
	}
	
	#endregion
}
